﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;

using edowork.com.SQL2005;

namespace WebInvoicingsys.system
{
    public class purchasein_putin_class
    {
        DataTable dtList;

        string connectionString;

        public decimal Total_qty { get; set; }

        private void getTotla()
        {
            Total_qty = 0;

            //统计 数量合计， 金额， 单价
            foreach (DataRow dr in dtList.Rows)
            {
                Total_qty = Total_qty + Decimal.Parse(dr["qty1"].ToString());
            }
        }

        public purchasein_putin_class(string connectionString)
        {
            this.connectionString = connectionString;

            dtList = new DataTable();

            dtList.Columns.Add("id");
            dtList.Columns.Add("areaid");
            dtList.Columns.Add("storageid");
            dtList.Columns.Add("areaname");
            dtList.Columns.Add("storagename");
            dtList.Columns.Add("productcode");
            dtList.Columns.Add("productname");
            dtList.Columns.Add("productshortname");
            dtList.Columns.Add("goodsclassname");
            dtList.Columns.Add("goodsunitname");
            dtList.Columns.Add("goodsspecname");
            dtList.Columns.Add("stackname");
            dtList.Columns.Add("guardname");
            dtList.Columns.Add("qty1");
            dtList.Columns.Add("qty2");
        }

        public bool Add(string areaid, string storageid, string productcode, string qty, ref string err_msg)
        {
            err_msg = "";

            decimal _qty;

            if (String.IsNullOrEmpty(areaid))
            {
                err_msg = Errormsg.purchasein_putin_Area_IsNullOrEmpty;
                return false;
            }

            if (String.IsNullOrEmpty(storageid))
            {
                err_msg = Errormsg.purchasein_putin_Storage_IsNullOrEmpty;
                return false;
            }

            if (String.IsNullOrEmpty(productcode))
            {
                err_msg = Errormsg.purchaseplan_product_IsNullOrEmpty;
                return false;
            }

            if (Decimal.TryParse(qty, out _qty) == false)
            {
                err_msg = Errormsg.purchaseplan_product_QtyFormatError;
                return false;
            }

            //检查是否已存在相同的货品条码            
            if (dtList.Select(String.Format("productcode='{0}' and storageid='{1}'", productcode, storageid)).Length > 0)
            {
                err_msg = Errormsg.purchaseplan_product_ExistProduct;
                return false;
            }

            //构造SQL语句
            string sql = @"select  '{1}' areaid, 
                                   (select areaname from area where areaid='{1}') areaname,
                                   '{2}' storageid,
                                   (select storagename from storage where storageid='{2}') storagename,
                                   a.productcode, a.productname, a.productshortname, a.goodsclassid, a.goodsunitid, a.goodsspecid, a.stackid, a.guardid, a.maxqty, a.minqty, a.sysdate,
                                   a.goodsclassname, a.goodsunitname, a.goodsspecname, a.stackname, a.guardname
                              from  v_product a
                                where a.productcode = '{0}'";

            sql = String.Format(sql, productcode, areaid, storageid);

            Execute exec = new Execute(connectionString);

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref err_msg);

            if((ds == null) || (ds.Tables.Count <= 0) || (ds.Tables[0].Rows.Count != 1))
            {
                err_msg = Errormsg.purchaseplan_product_NotFoundProduct;
                return false;
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                DataRow newdr = dtList.NewRow();

                newdr["id"] = DateTime.Now.Ticks.ToString();

                newdr["areaid"] = dr["areaid"].ToString();
                newdr["storageid"] = dr["storageid"].ToString();
                newdr["areaname"] = dr["areaname"].ToString();
                newdr["storagename"] = dr["storagename"].ToString();

                newdr["productcode"] = dr["productcode"].ToString();
                newdr["productname"] = dr["productname"].ToString();
                newdr["productshortname"] = dr["productshortname"].ToString();
                newdr["goodsclassname"] = dr["goodsclassname"].ToString();
                newdr["goodsunitname"] = dr["goodsunitname"].ToString();
                newdr["goodsspecname"] = dr["goodsspecname"].ToString();
                newdr["stackname"] = dr["stackname"].ToString();
                newdr["guardname"] = dr["guardname"].ToString();
                newdr["qty1"] = _qty;
                newdr["qty2"] = "0";

                dtList.Rows.Add(newdr);
            }

            getTotla();

            return true;
        }

        public void Reset()
        {
            dtList.Rows.Clear();

            getTotla();
        }

        public void Delect(string id)
        {
            DataRow[] drFind = dtList.Select(String.Format("id='{0}'", id));

            foreach (DataRow dr in drFind)
            {
                dtList.Rows.Remove(dr);
            }

            getTotla();
        }

        public DataTable getList()
        {
            return this.dtList;
        }

        public int getListTotal()
        {
            return dtList.Rows.Count;
        }

        public bool getDataList(string id)
        {
            string err_msg = "";

            dtList.Rows.Clear();

            //构造SQL语句
            string sql = @"select c.areaid, c.areaname, c.storageid, c.storagename,  b.productcode, b.productname, b.productshortname, b.goodsclassname, b.goodsunitname, b.goodsspecname, b.stackname, b.guardname, a.qty1, a.qty2
                              from billsdetail a, v_product b, v_storage c
                               where a.productcode=b.productcode
                                 and a.storageid=c.storageid
                                 and a.billsno='{0}'";

            sql = String.Format(sql, id);

            Execute exec = new Execute(connectionString);

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref err_msg);

            if (ds == null)
            {
                return false;
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                DataRow newdr = dtList.NewRow();

                newdr["id"] = (DateTime.Now.Ticks + dtList.Rows.Count).ToString();

                newdr["areaid"] = dr["areaid"].ToString();
                newdr["storageid"] = dr["storageid"].ToString();
                newdr["areaname"] = dr["areaname"].ToString();
                newdr["storagename"] = dr["storagename"].ToString();

                newdr["productcode"] = dr["productcode"].ToString();
                newdr["productname"] = dr["productname"].ToString();
                newdr["productshortname"] = dr["productshortname"].ToString();
                newdr["goodsclassname"] = dr["goodsclassname"].ToString();
                newdr["goodsunitname"] = dr["goodsunitname"].ToString();
                newdr["goodsspecname"] = dr["goodsspecname"].ToString();
                newdr["stackname"] = dr["stackname"].ToString();
                newdr["guardname"] = dr["guardname"].ToString();
                newdr["qty1"] = dr["qty1"].ToString();
                newdr["qty2"] = dr["qty2"].ToString();

                dtList.Rows.Add(newdr);
            }

            getTotla();

            return true;
        }
    }
}